package com.wanmait.recycle.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wanmait.recycle.mapper.RegCountMapper;
import com.wanmait.recycle.mapper.UserinfoMapper;
import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.RegCount;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.MD5Util;
import com.wanmait.recycle.util.Pager;

@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {

	
	@Resource
	private UserinfoMapper userinfoMapper;
	@Resource
	private RegCountMapper regCountMapper;
	
	@Override
	public void insert(Userinfo object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Userinfo object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(String ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Userinfo findById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Userinfo> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDataCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Userinfo> findAll(Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Userinfo> findAll(Userinfo object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDataCount(Userinfo object) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Userinfo> findAll(Pager pager, Userinfo object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Userinfo> findAllByExample(Userinfo userinfo) {
		// TODO Auto-generated method stub
		
		return userinfoMapper.findAllByExample(userinfo);
	}

	@Override
	public Userinfo selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return userinfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Userinfo> findAllBySearch(String searchStr,String beginTime, String endTime,String enable) {
		// TODO Auto-generated method stub
		return userinfoMapper.findAllBySearch(searchStr, beginTime, endTime,enable);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		userinfoMapper.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		// TODO Auto-generated method stub
		
		ids = ids.substring(0, ids.length()-1);
		String[] nn =ids.split(",");
		
		userinfoMapper.deleteByIds(nn);
	}

	@Override
	public void restoreByIds(String ids) {
		// TODO Auto-generated method stub
		ids = ids.substring(0, ids.length()-1);
		String[] nn =ids.split(",");
		
		userinfoMapper.restoreByIds(nn);
	}

	@Override
	public void restoreById(int i) {
		// TODO Auto-generated method stub
		userinfoMapper.restoreById(i);
	}

	@Override
	public List<Userinfo> findAllApprovePic() {
		// TODO Auto-generated method stub
		return userinfoMapper.findAllApprovePic();
	}

	@Override
	public void passByIds(String ids,Admin admin) {
		
		ids = ids.substring(0, ids.length()-1);
		String[] nn =ids.split(",");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		// new Date()为获取当前系统时间
		
		
		userinfoMapper.passByIds(nn,admin,df.format(new Date()));
		
	}

	@Override
	public void passById(Integer id,Admin admin) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		// new Date()为获取当前系统时间
		
		userinfoMapper.passById(id,admin,df.format(new Date()));
		
	}

	@Override
	public void unpassById(Integer id,Admin admin) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		// new Date()为获取当前系统时间

		userinfoMapper.unpassById(id,admin,df.format(new Date()));
		
	}

	@Override
	public Userinfo findUserinfoByExample2(Userinfo userinfo) {		
		
		
		return userinfoMapper.findUserByExample(userinfo);
	}
	
	@Override
	public Userinfo findUserinfoByExample(Userinfo userinfo) {
		
		String password = userinfo.getPassword();
		
		String newpass = MD5Util.md5(password);
		userinfo.setPassword(newpass);
		
		return userinfoMapper.findUserByExample(userinfo);
	}

	@Override
	public void saveUserinfo(Userinfo userinfo) {
		
		//密码加密
		String password = userinfo.getPassword();
		
		String newpass = MD5Util.md5(password);
		
		userinfo.setPassword(newpass);
		
		userinfoMapper.saveUserinfo(userinfo);
		
	}

	@Override
	public boolean isUserinfoByExample(Userinfo userinfo) {
		
		boolean f = false;
		if(userinfoMapper.isUserinfoByExample(userinfo)==null) {
			f = false;
		}else {
			f = true;
		}
		
		return  f;
	}
	
	@Override
	public Userinfo findAllById(Integer id) {
		//通过id寻找用户具体信息 3.4 ad
		return userinfoMapper.findAllById(id);
	}


	@Override
	public void updateMailById(Integer id, String mailBox) {
		// TODO Auto-generated method stub
		userinfoMapper.updateMailById(id, mailBox);
	}

	@Override
	public void updatePhoneById(Integer id, String phone) {
		// TODO Auto-generated method stub
		userinfoMapper.updatePhoneById(id, phone);
	}

	@Override
	public void updateHeadNameById(Integer id, String headName) {
		// TODO Auto-generated method stub
		//通过id更改用户头像 3.4 ad
		userinfoMapper.updateHeadNameById(id, headName);
	}

	@Override
	public void updateCompanyById(Userinfo userinfo) {
		userinfoMapper.updateCompanyById(userinfo);
	}

	@Override
	public void updatePasswordById(Integer id, String password) {
		// TODO Auto-generated method stub
		String mdpass = MD5Util.md5(password);
		userinfoMapper.updatePasswordById(id, mdpass);
	}

	@Override
	public Userinfo isHaveUser(String str,String mother) {
		
		Userinfo user = new Userinfo();
		Userinfo userinfo = null;
		if("name".equals(mother)) {
			user.setName(str);
			userinfo = userinfoMapper.isUserinfoByExample(user);
		}
		if("mailbox".equals(mother)) {
			user.setMailBox(str);
			userinfo = userinfoMapper.isUserinfoByExample(user);
		}
		
		return userinfo;
		
	}

	@Override
	public void updatePasswordByName(String name, String newPass) {
		
		String mdpass = MD5Util.md5(newPass);
		userinfoMapper.updatePasswordByName(name,mdpass);
		
	}

	@Override
	public List<Userinfo> findMore(String ids) {
		
		ids = ids.substring(0, ids.length()-1);
		String[] nn =ids.split(",");
		
		List<Userinfo> userinfos = userinfoMapper.findMore(nn);
		return userinfos;
	}

	@Override
	public List<RegCount> selectUserCount() {
		// TODO Auto-generated method stub
		return regCountMapper.selectUserCount();
	}

	/*用户每次登录，更新最后一次登录时间，记录最后一次登录ip,登录次数加1 ZQ*/
	@Override
	public void updateIp(Userinfo userinfo) {
		userinfoMapper.updateIp(userinfo);
		
	}

	/*发布文章数量、贡献、提问、回答、收藏数量 ZQ*/
	@Override
	public void updateCount(Userinfo userinfo) {
		if(userinfo!=null) {
			userinfoMapper.updateCount(userinfo);
		}
		
	}
}
